﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APPLICATION.Departments
{
    public partial class frmADD : Form
    {
        private int intMgrID;
        private string privatestrParentName;

        public frmADD(int intMgr, string strParentName)
        {
            InitializeComponent();
            txtParentName.Text = strParentName;
            privatestrParentName = strParentName;
            txtName.Focus();

            intMgrID = intMgr;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlCommand cmdSQL = new System.Data.SqlClient.SqlCommand();
            cmdSQL.CommandText = "tsp_dtDep";
            cmdSQL.CommandType = CommandType.StoredProcedure;
            cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 0;
            cmdSQL.Parameters.Add("@hName", SqlDbType.NVarChar,50).Value = txtName.Text.Trim();

            if (ckbNoReferrer.Checked == false)
            {
                cmdSQL.Parameters.Add("@mgrid", SqlDbType.Int).Value = intMgrID;
            }
            //cmdSQL.Parameters.Add("@CodeID", SqlDbType.Int).Value = intCodeID;

            System.Data.SqlClient.SqlConnection cnSQL = new System.Data.SqlClient.SqlConnection();
            cnSQL.ConnectionString = globals.APPCONNSTRING;
            try
            {
                cnSQL.Open();
                cmdSQL.Connection = cnSQL;
                cmdSQL.ExecuteNonQuery();
                cnSQL.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Произошла ошибка во время сохранения! \n" + ex.Message, "TIMS");
                this.Close();
            }

        }

        private void ckbNoReferrer_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNoReferrer.Checked == true)
            {
                txtParentName.Text = "";
            }
            else
            {
                txtParentName.Text = privatestrParentName;
            }
        }
    }
}
